﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR; 
using SignalR.Web.Hubs;
using SignalR.Web.LogHelper;
using SignalR.Web.Models;
using System.Text;

namespace SignalR.Web.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class TestController : ControllerBase
    {
        private readonly IHubContext<ChatHub> _hubContext;
        private readonly ILogger<TestController> _logger;
 
        public TestController(IHubContext<ChatHub> hubContext,ILogger<TestController> logger)
        {
            _hubContext = hubContext;
            _logger = logger; 
        }
        [HttpGet]
        public async Task<string> GetCount()
        {
            var sb =new StringBuilder();
            foreach (var item in WebConst.OnlineUserList)
            {
                sb.Append(item.ConnectionId);
                sb.Append(item.UserName);
                sb.Append(item.ActiveStatus);
                sb.Append("==");
            }
            return await Task.FromResult(sb.ToString());
        }

        [HttpGet]
        public async Task<int> Get()
        {
            _logger.LogInformation("系统通知：ReceiveMessage");
            await _hubContext.Clients.All.SendAsync("ReceiveMessage", "系统通知", $"北京时间{DateTime.Now}");
            return 0;
        }

        [HttpGet]
        public async Task  SendToUser(string user)
        {
            var model = WebConst.OnlineUserList.Find(u => u.UserName == user);
            if (model != null)
                await  _hubContext.Clients.Client(model.ConnectionId).SendAsync("ReceiveMessage", $"上海时间{DateTime.Now}"); 
            else
                await _hubContext.Clients.All.SendAsync("ReceiveMessage", "系统通知", $"北京时间{DateTime.Now}");
        }

        [HttpGet]
        public  async Task<JsonResult> GetLogMessage()
        {
            var data = ReadHelper.Read();
            await _hubContext.Clients.All.SendAsync("ReceiveLog", data);
            return new JsonResult(0);
        }

        /// <summary>
        /// 获取所在组的在线用户
        /// </summary>
        public async Task GetUsers()
        { 
            ResultModel result = new ResultModel();

            result.Code = 0;
            result.Msg = "OnlineUserList";
            result.OnlineUser = WebConst.OnlineUserList;
            //给所在组返回消息
            await _hubContext.Clients.All.SendAsync("GetUsersResponse", result);
        }

        public async Task SendMessage(string user)
        {
            ResultModel result = new ResultModel();
            var model = WebConst.OnlineUserList.Find(u => u.UserName == user);
            if (model == null)
            {
                result.Code = 1;
                result.Msg = "请先登录！";
                return;
            }
            else
            {
                result.Code = 0;
                result.Msg = $"“{user}”发送的消息：上海测试";
            }
            
            await _hubContext.Clients.Client(model.ConnectionId).SendAsync("SendMessageResponse", result);
        }
    }
     
}